<template>
  <div class="app-container">
    <div style="margin:0 0 15px 0">
      <el-button
        size="small"
        type="primary"
        @click="addBrand"
        v-permission="['admin/product/cate/create']"
        >添加</el-button
      >
      <el-button
        size="small"
        type="primary"
        @click="editBrand"
        v-permission="['admin/product/cate/update']"
        >修改</el-button
      >
      <el-button
        size="small"
        type="primary"
        @click="deleteBrand"
        v-permission="['admin/product/cate/del']"
        >删除</el-button
      >
    </div>
    <el-table
      :data="dataList"
      v-loading="listLoading"
      border
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      row-key="id"
      highlight-current-row
      style="width: 100%"
      @current-change="handleCurrentChange"
    >
      <el-table-column property="text" label="分类名称"> </el-table-column>
      <el-table-column property="typeName" label="商品类型"> </el-table-column>
      <el-table-column property="scaling" label="分佣比例"> </el-table-column>
      <el-table-column property="sort" label="权重"> </el-table-column>
    </el-table>
  </div>
</template>
<script>
import { cateList, deleteCate } from "@/api/product";
import { Message } from "element-ui";
export default {
  name: "Brand",
  data() {
    return {
      dataList: [],
      childrenData: [],
      listLoading: false,
      currentRow: null
    };
  },
  created() {
    this.getList();
  },
  methods: {
    load(tree, treeNode, resolve) {
      setTimeout(() => {
        resolve(this.childrenData);
      }, 1000);
    },
    handleCurrentChange(val) {
      // 选中当前行数据
      this.currentRow = val;
    },
    // 获取列表数据
    getList() {
      this.listLoading = true;
      cateList({ id: 0 }).then(res => {
        this.dataList = res.data;
        this.listLoading = false;
      });
    },
    reset() {
      this.getList();
    },
    addBrand() {
      this.$router.push({ path: "/product/cateadd" });
    },
    editBrand() {
      if (!this.currentRow) {
        Message.error("请选择操作行。");
        return;
      }
      this.$router.push({
        path: "/product/cateedit",
        query: { id: this.currentRow.id }
      });
    },
    deleteBrand() {
      if (!this.currentRow) {
        Message.error("请选择操作行。");
        return;
      }
      this.$confirm("确定删除吗？", "提示", {
        onfirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        deleteCate({ id: this.currentRow.id }).then(res => {
          if (res.data.success) {
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.getList();
          } else {
            this.$message({
              type: "error",
              message: res.data.message
            });
          }
        });
      });
    }
  }
};
</script>
<style scoped>
.filter-container .el-col-6 {
  margin-bottom: 15px;
}
.filter-item {
  margin-bottom: 0;
}
/deep/ .filter-container label {
  font-size: 14px;
  font-weight: 500;
  margin-right: 10px;
}
</style>
